package light.mvc.service.record.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.record.Troom;
import light.mvc.model.sys.Tdictionarytype;
import light.mvc.model.sys.Tresource;
import light.mvc.model.sys.Trole;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.meetingroom.RoomPageModel;
import light.mvc.pageModel.record.Room;
import light.mvc.pageModel.sys.Role;
import light.mvc.service.record.RoomServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoomServiceImpl implements RoomServiceI {
	@Autowired
	private BaseDaoI<Troom> roomDao;

	@Override
	public List getAllRoom() {
		return roomDao.find("from Troom");
	}

	@Override
	public List<Room> getAllRoomUsedForDel() {
		List<Troom> trooms = roomDao.find("from Troom");
		List<Room> ul = new ArrayList<Room>();
		for (Troom t : trooms) {
			Room u = new Room();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	}

	@Override
	public List<Room> dataGrid(Room room, PageFilter ph) {
		List<Room> ul = new ArrayList<Room>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Troom t ";
		List<Troom> l = this.roomDao.find(hql);
		for (Troom t : l) {
			Room u = new Room();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	}

	private String whereHql(Room room, Map<String, Object> params) {
		String hql = "";
		if (room != null) {
			hql = hql + " where 1=1 ";
			if (room.getName() != null) {
				hql = hql + " and t.name like :name";
				params.put("name", "%%" + room.getName() + "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	public Long count(Room room, PageFilter ph) {
		Map<String, Object> params = new HashMap();
		String hql = " from Troom t ";
		return this.roomDao.count("select count(*) " + hql + whereHql(room, params), params);
	}

	@Override
	public Room getById(long id) {
		Room room = new Room();

		Troom u = this.roomDao.get(Troom.class, id);
		BeanUtils.copyProperties(u, room);
		return room;
	}

	@Override
	public Room getByName(String name) {
		Room room = new Room();

		Troom u = this.roomDao.get("from Troom t where t.name=" + name);
		BeanUtils.copyProperties(u, room);
		return room;

	}

	@Override
	public void add(Room r) {
		Troom t = new Troom();
		t.setName(r.getName());
		// t.setDescription(r.getDescription());
		roomDao.save(t);
	}

	@Override
	public void delById(Long id) {
		Troom t = roomDao.get(Troom.class, id);
		this.roomDao.delete(t);
	}

	@Override
	public void edit(RoomPageModel r) {
		Troom t = roomDao.get(Troom.class, Long.valueOf(r.getRoomId()));
		t.setName(r.getRoomName());
		roomDao.update(t);
	}
}
